package wj.d913;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

public class P1 {
    public static void main(String[] args) {
        int[] date = new int[]{2021, 9, 13, 0, 0, 0};
        Date dd = new Date("2021-9-13 0:0:0");


        Calendar calendar= Calendar.getInstance();
        //
        exchange(date, "+3h");
        Arrays.stream(date).forEach(s -> {
            System.out.print(s + " ");
        });
        System.out.println();
    }

    // y-M-d h:m:s
    public static void exchange(int[] date, String optStr) {
        char firstChar = optStr.charAt(0);
        char lastChar = optStr.charAt(optStr.length() - 1);
        int num = Integer.parseInt(optStr.substring(1, optStr.length()-1));
        switch (lastChar) {
            case 's': {
                if ('-' == firstChar) {
                    if (date[5] > num) {
                        date[5] -= num;
                    } else {
                        exchange(date, "-1m");
                        date[5] += 60 - num;
                    }
                } else {
                    if (date[5] + num < 60) {
                        date[5] += num;
                    } else {
                        exchange(date, "+1m");
                        date[5] = (date[5] + num) % 60;
                    }
                }
            }
            break;
            case 'm': {
                if ('-' == firstChar) {
                    if (date[4] > num) {
                        date[4] -= num;
                    } else {
                        exchange(date, "-1h");
                        date[4] += 60 - num;
                    }
                } else {
                    if (date[4] + num < 60) {
                        date[4] += num;
                    } else {
                        exchange(date, "+1h");
                        date[4] = (date[4] + num) % 60;
                    }
                }
            }
            break;
            case 'h': {
                if ('-' == firstChar) {
                    if (date[3] > num) {
                        date[3] -= num;
                    } else {
                        exchange(date, "-1d");
                        date[3] += 24 - num;
                    }
                } else {
                    if (date[3] + num < 24) {
                        date[3] += num;
                    } else {
                        exchange(date, "+1d");
                        date[3] = (date[4] + num) % 24;
                    }
                }
            }
            break;
            case 'W': {
                num = num * 7;
            }
            case 'd': {
                if ('-' == firstChar) {
                    if (date[2] > num) {
                        date[2] -= num;
                    } else {
                        exchange(date, "-1M");
                        if (date[1] % 2 == 1 && date[1] != 11) {
                            date[2] += 30 - num;
                        } else {
                            date[2] += 31 - num;
                        }
                    }
                } else {
                    int board = 31;
                    if (date[1] % 2 == 1 && date[1] != 11) {
                        board = 30;
                    }
                    if (date[2] + num < board) {
                        date[2] += num;
                    } else {
                        exchange(date, "+1M");
                        date[2] = (date[2] + num) % 24;
                    }
                }
            }
            break;
        }
    }

}
